﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SilverLightStreamingInterface;

namespace Deep_Talk_Converter.RenderWorkItems
{
    class RenderMeetingSL
    {
        private string _url;
        private string _id, _key;
        private string _name;

        public RenderMeetingSL(string url, string slID, string slKey, string slProjectName)
        {
            _url = url;
            _id = slID;
            _key = slKey;
            _name = slProjectName;
        }

        public delegate void MeetingNameCallback(string meetingName);

        public delegate void ProgressMessageCallback (string message);

        public delegate void RenderDoneCallback(bool ok, string message, string uri);

        /// <summary>
        /// Called when the meeting name is known.
        /// </summary>
        public event MeetingNameCallback MeetingNameKnown;

        /// <summary>
        /// Called when we are notifying people of progress.
        /// </summary>
        public event ProgressMessageCallback ProgressMessage;

        public event RenderDoneCallback RenderDone;

        private RenderMeetingLocally _local;

        /// <summary>
        /// Actually run the render. We first do the normal render, actually, and then upload everything to SL.
        /// </summary>
        internal void Render()
        {
            _local = new RenderMeetingLocally(_url);
            _local.MeetingNameKnown += new RenderMeetingLocally.MeetingNameCallback(local_MeetingNameKnown);
            _local.ProgressMessage += new RenderMeetingLocally.ProgressMessageCallback(local_ProgressMessage);
            _local.RenderDone += new RenderMeetingLocally.RenderDoneCallback(local_RenderDone);
            _local.Render();

        }

        /// <summary>
        /// Continue with the render.
        /// </summary>
        /// <param name="ok"></param>
        /// <param name="message"></param>
        /// <param name="uri"></param>
        void local_RenderDone(bool ok, string message, string uri)
        {
            if (!ok)
            {
                SendRenderDone(ok, message, uri);
                return;
            }

            ///
            /// Great - now we can go ahead and grab the project and ship it off to SL.
            /// 

            SendProgressMessage("Cleaning directory of illegal Silverlight files");
            DirectoryInfo project = _local.ProjectDir;
            SLPackage pkg = new SLPackage(_name) { PackageDirectory = project };

            ///
            /// Generate that html file we are going to be using!
            /// 

            PackagePrep prep = new PackagePrep();
            FileInfo webFile = prep.CreateSLWebFile(pkg, _id.Trim(), _local.Title);

            ///
            /// If the app isn't there, then create it. Otherwise, we just do an "update".
            /// 

            SendProgressMessage("Checking the Streaming Service");
            SLLiveInterface live = new SLLiveInterface(_id.Trim(), _key.Trim());
            if (!live.PackageExists(pkg)) {
                SendProgressMessage("Creating new Streaming App");
                live.CreateProvisionedPackage(pkg.Name, new FileInfo(pkg.PackageDirectory.FullName + "\\manifest.xml"));
                FileUploadStateCache fc = new FileUploadStateCache();
                fc.ClearCacheStatus(pkg.PackageDirectory);
            }

            SendProgressMessage("Starting file upload");
            live.UploadPackage(pkg, (fname, mok) => { SendProgressMessage("Uploaded " + fname.Substring(fname.LastIndexOf("\\")+1)); });

            SendRenderDone(true, "Done!", webFile.FullName);
        }

        void local_ProgressMessage(string message)
        {
            SendProgressMessage(message);
        }

        void local_MeetingNameKnown(string meetingName)
        {
            SendMeetingKnown(meetingName);
        }

        private void SendProgressMessage(string p)
        {
            if (ProgressMessage != null)
            {
                ProgressMessage(p);
            }
        }

        private void SendMeetingKnown(string p)
        {
            if (MeetingNameKnown != null)
            {
                MeetingNameKnown(p);
            }
        }

        private void SendRenderDone(bool ok, string message, string uri)
        {
            if (RenderDone != null)
            {
                RenderDone(ok, message, uri);
            }
        }
    }
}
